package com.neo.sk.collector

import java.util.concurrent.{SynchronousQueue, ThreadPoolExecutor, TimeUnit}

import akka.actor.{Actor, ActorSystem, Props}
import com.neo.sk.common.{UrlTask, Launcher}
import org.slf4j.LoggerFactory
import play.api.libs.json.Json

import scala.concurrent.ExecutionContext

/**
 * User: Huangshanqi
 * Date: 2015/6/4
 * Time: 11:34
 */
class Manager extends Actor{

  private val logger = LoggerFactory.getLogger(this.getClass)


/*
  val fetchTagActor = context.actorOf(Props[MovieTagFetchActor](new MovieTagFetchActor), "fetchTagActor-" + System.currentTimeMillis())
  private final val fetchTagUrl = "http://movie.douban.com/tag/?view=cloud&retry=1"
  */

/*
  private val fetchMovieUrlManagerActor = context.actorOf(Props[FetchMovieUrlManager](new FetchMovieUrlManager),"fetchMovieUrlManagerActor-1")
*/

  private val fetchMovieDetailManager = context.actorOf(Props[FetchMovieDetailManager](new FetchMovieDetailManager),"FetchMovieDetailManager-1")


  override def receive: Receive = {
    case Launcher =>{
      logger.info("Launcher...........................")
//      fetchTagActor ! UrlTask(fetchTagUrl)
//      fetchMovieUrlManagerActor ! Launcher
      fetchMovieDetailManager ! Launcher
    }
    case unknow@_ => logger.info("unknow message: "+ unknow + "  in " + context.self.path.name + " from " + context.sender().path.name)
  }


}

object Manager{
  private val logger = LoggerFactory.getLogger(this.getClass)


  val pool = new ThreadPoolExecutor(0, 4096,
    60L, TimeUnit.SECONDS,
    new SynchronousQueue[Runnable]()
  )
  implicit val myExecutionContext: ExecutionContext =
    ExecutionContext.fromExecutorService(pool)


  implicit val system = ActorSystem("CrawlerSystem")

  val manager = system.actorOf(Props[Manager])

  def start()={
    manager ! Launcher
    logger.info("CrawlerSystem launched........................................")
  }

  def jsonResult(code: Int, msg: String) =
    Json.obj(
      "Result" -> code,
      "Messages" -> msg
    ).toString()
}
